home *** CD-ROM | disk | FTP | other *** search
/ BBS Toolkit / BBS Toolkit.iso / maximus / mul100.zip / MAZE.SCR < prev    next >
Text File  |  1993-02-01  |  7KB  |  228 lines

  1.  
  2. // MAZE.SCR  --  Generate and display a random maze  --  Version 1.00
  3. //
  4. // Script program for MUL - the Maximus User Language
  5. // MUL is (C) Copyright 1990-93 by CodeLand Australia
  6. //
  7. // Based on code placed in the Public Domain by Jonathan Guthrie.
  8. //
  9. // USAGE:       MUL -pMaze                      or
  10. //              MUL -pMaze <Cols> <Rows>
  11.  
  12. char *banner = "MAZE v1.00";                // Script banner
  13. char *aborts = "\nKeyboard ABORT\n";        // Keyboard abort
  14.  
  15. int CSIZ=19;                                // Default columns dimension
  16. int RSIZ=4;                                 // Default rows dimension
  17. // int CSIZ=39;                             // Default columns dimension
  18. // int RSIZ=8;                              // Default rows dimension
  19.  
  20. char SCH = 0x20;                            // Space display character
  21. //char MCH = '#';                           // Ascii Display character
  22. char MCH = 0xDB;                            // IBM graphic display character
  23. //char MCH = 0xB2;                          // IBM graphic display character
  24.  
  25. int array[(CSIZ+2)*(RSIZ+2)];               // Maze array
  26.  
  27. int UP=1;                                   // Maze generation constant
  28. int DN=2;                                   // Maze generation constant
  29. int LT=4;                                   // Maze generation constant
  30. int RT=8;                                   // Maze generation constant
  31. int keybreak=0;                             // Keyboard abort
  32. int rotor;                                  // Rotor display flag
  33.  
  34. main (int argc, char *arg1, char *arg2)     // Main program
  35. {
  36.     int r, c, base;
  37.     long search[CSIZ*RSIZ+((CSIZ*RSIZ)/2)];
  38.  
  39.     getcmdline (argc,arg1,arg2);            // Get the command line
  40.  
  41.     // Announce
  42.     printf ("\n%s - Generate a %d column by %d row Maze\n",banner,CSIZ,RSIZ);
  43.  
  44.     // Setup the maze array
  45.     for (c=0;c<CSIZ+1;c++) {
  46.         array[c*(RSIZ+2)]=-1; array[RSIZ+1+c*(RSIZ+2)]=-1;
  47.     }
  48.     for (r=1;r<RSIZ+1;r++) {
  49.         array[r]=-1; array[r+(CSIZ+1)*(RSIZ+2)]=-1;
  50.         for (c=1;c<CSIZ+1;c++) array[r+c*(RSIZ+2)]=0;
  51.     }
  52.  
  53.     srand (SysTime ());                     // Setup the random generator
  54.     r=rand ()%RSIZ+1; c=rand ()%CSIZ+1;
  55.     base=addelem (0,search,r,c);
  56.     array[r+c*(RSIZ+2)]=RT+RT;              // Not a valid value
  57.  
  58.     printf ("\nGenerating the maze .. ");   // Notify
  59.  
  60.     while (0<base) {                        // Create the maze
  61.         r=rand ()%base;
  62.         c=search[r]; search[r]=search[--base];
  63.         r=c%(CSIZ*RSIZ); c=c/(CSIZ*RSIZ);
  64.         openwall (r,c);
  65.         base=addelem (base,search,r,c);
  66.         if (keyabort ()) break;
  67.     }
  68.  
  69.     if(!keybreak) writemaze ();             // Display the maze
  70.  
  71.     saybibi ();                             // Was it good for you too?
  72. }
  73.  
  74. addelem (int base, long *search, int row, int col)
  75. {
  76.     if (0==array[row-1+col*(RSIZ+2)]) {
  77.         search[base++]=row+col*(CSIZ*RSIZ)-1;
  78.         array[row-1+col*(RSIZ+2)]=-DN;
  79.     }
  80.     else if(0>array[row-1+col*(RSIZ+2)])
  81.         array[row-1+col*(RSIZ+2)]=array[row-1+col*(RSIZ+2)]-DN;
  82.  
  83.     if(0==array[row+1+col*(RSIZ+2)]) {
  84.         search[base++]=row+col*(CSIZ*RSIZ)+1;
  85.         array[row+1+col*(RSIZ+2)]=-UP;
  86.     }
  87.     else if(0>array[row+1+col*(RSIZ+2)])
  88.         array[row+1+col*(RSIZ+2)]=array[row+1+col*(RSIZ+2)]-UP;
  89.  
  90.     if(0==array[row+(col-1)*(RSIZ+2)]) {
  91.         search[base++]=row+col*(CSIZ*RSIZ)-(CSIZ*RSIZ);
  92.         array[row+(col-1)*(RSIZ+2)]=-RT;
  93.     }
  94.     else if(0>array[row+(col-1)*(RSIZ+2)])
  95.         array[row+(col-1)*(RSIZ+2)]=array[row+(col-1)*(RSIZ+2)]-RT;
  96.  
  97.     if(0==array[row+(col+1)*(RSIZ+2)]) {
  98.         search[base++]=row+col*(CSIZ*RSIZ)+(CSIZ*RSIZ);
  99.         array[row+(col+1)*(RSIZ+2)]=-LT;
  100.     }
  101.     else if(0>array[row+(col+1)*(RSIZ+2)])
  102.         array[row+(col+1)*(RSIZ+2)]=array[row+(col+1)*(RSIZ+2)]-LT;
  103.  
  104.     return base;
  105. }
  106.  
  107. openwall (int row, int col)
  108. {
  109.     int directions, max, direction, temprow, tempcol, temp, back;
  110.  
  111.     directions=-array[row+col*(RSIZ+2)];
  112.     max=0;
  113.  
  114.     if (And (directions,UP)) {
  115.         temp=rand ();
  116.         if (temp>max) {
  117.             max=temp; direction=UP; back=DN;
  118.             temprow=row-1; tempcol=col;
  119.         }
  120.     }
  121.  
  122.     if(And (directions,DN)) {
  123.         temp=rand ();
  124.         if(temp>max) {
  125.             max=temp; direction=DN; back=UP;
  126.             temprow=row+1; tempcol=col;
  127.         }
  128.     }
  129.  
  130.     if(And (directions,LT)) {
  131.         temp=rand ();
  132.         if(temp>max) {
  133.             max=temp; direction=LT; back=RT;
  134.             temprow=row; tempcol=col-1;
  135.         }
  136.     }
  137.  
  138.     if(And (directions,RT)) {
  139.         temp=rand ();
  140.         if(temp>max) {
  141.             max=temp; direction=RT; back=LT;
  142.             temprow=row; tempcol=col+1;
  143.         }
  144.     }
  145.  
  146.     array[row+col*(RSIZ+2)]=direction;
  147.     array[temprow+tempcol*(RSIZ+2)]=array[temprow+tempcol*(RSIZ+2)]+back;
  148. }
  149.  
  150. // Write the maze to screen
  151. writemaze ()
  152. {
  153.     int r, c;
  154.  
  155.     printf (" \n\n");                         // Rotor cleanup
  156.     array[RSIZ+CSIZ*(RSIZ+2)]=DN;             // Lower door
  157.  
  158.     for (c=1;c<CSIZ+1;++c) {                  // Display top line
  159.         if(c!=1) printf ("%c%c",MCH,MCH);
  160.         else printf ("%c%c",MCH,SCH);         // Upper door
  161.     }
  162.     printf("%c\n",MCH);
  163.  
  164.     for (r=1;r<RSIZ+1;++r) {                  // Display maze body
  165.         putch (MCH);
  166.         for (c=1;c<CSIZ+1;++c) {
  167.               putch (SCH);
  168.               if (And (array[r+c*(RSIZ+2)],RT)) putch (SCH);
  169.               else putch (MCH);
  170.         }
  171.         putch ('\n');
  172.  
  173.         for (c=1;c<CSIZ+1;++c) {
  174.               putch (MCH);
  175.               if (And (array[r+c*(RSIZ+2)],DN)) putch (SCH);
  176.               else putch (MCH);
  177.         }
  178.         printf ("%c\n",MCH);
  179.     }
  180. }
  181.  
  182. // Display the rotor 8-)
  183. showrotor ()
  184. {
  185.      if (++rotor>8) rotor=1;
  186.      
  187.      if (rotor==2)      puts ("-\b");
  188.      else if (rotor==4) puts ("\\\b");
  189.      else if (rotor==6) puts ("|\b");
  190.      else if (rotor==8) puts ("/\b");
  191. }
  192.  
  193. // Check for keyboard abort
  194. keyabort ()
  195. {
  196.     if (kbhit ()) {                         // Abort if key hit
  197.         getch ();                           // Get the character
  198.         putch (' ');                        // Rotor cleanup
  199.         puts (aborts);                      // Acknowledge abort
  200.         ++keybreak;                         // Flag an abort
  201.         return 1;                           // Exit play loop
  202.     }
  203.  
  204.     showrotor ();                           // Display activity
  205.     return 0;
  206. }
  207.  
  208. // Get the command line
  209. getcmdline (int argc, char *a1, char *a2)
  210. {
  211.     // Load command line maze size options
  212.  
  213.     if (argc) CSIZ=atoi (a1);               // Maze columns dimension
  214.     if (argc>1) RSIZ=atoi (a2);             // Maze rows dimension
  215.  
  216.     if (CSIZ<2 || CSIZ>39) CSIZ=39;         // Bounds check
  217.     if (RSIZ<2 || RSIZ>60) CSIZ=8;          // Bounds check
  218. }
  219.  
  220. // Byebye
  221. saybibi ()
  222. {                             
  223.     puts ("\nMaze done!\n");
  224. }
  225.  
  226. // End of script
  227.  
  228.